/**
 * 
 */
package com.maywide.jdp.system.bean;

import java.io.Serializable;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.persistence.Query;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import com.maywide.jdp.core.exception.AppException;
import com.maywide.jdp.core.vo.Page;
import com.maywide.jdp.dao.Dao;
import com.maywide.jdp.dao.PageQueryTemplate;
import com.maywide.jdp.system.entities.Parameter;

/**
 * 参数管理Bean
 * 
 * @author luoyang
 * @version 1.0
 * @since JDK1.6
 */
@Component("parameterBean")
@Transactional(propagation=Propagation.REQUIRED)
public class ParameterBeanImpl implements ParameterBean {
	/**
	 * 基础DAO
	 */
	private Dao dao = null;

	/**
	 * @param dao the dao to set
	 */
	@Autowired
	public void setDao(Dao dao) {
		this.dao = dao;
	}

	@Override
	public void insert(Object obj) throws Exception {
		dao.insert(obj);
	}

	@SuppressWarnings({ "unchecked", "rawtypes" })
	@Override
	public void remove(Class clazz,Serializable id) throws Exception {
		Object obj = dao.find(clazz, id);
		dao.remove(obj);
	}

	@Override
	public void update(Object obj) throws Exception {
		dao.update(obj);
	}

	@Override
	@Transactional(propagation=Propagation.NOT_SUPPORTED)
	public Object load(Serializable id) throws Exception {
		Parameter button = dao.find(Parameter.class, id);
		return button;
	}

	@Override
	public void lock(String id) throws Exception {
		String jql = "update Parameter as o set o.active = false where o.id = :id";
		Query query = dao.createQuery(jql);
		query.setParameter("id", id);
		query.executeUpdate();
	}

	@Override
	public void lockAll(String[] ids) throws Exception {
		String hql = "update Parameter as o set o.active = false where o.id in (:ids)";
		Map<String, Object> conditions = new HashMap<String,Object>();
		conditions.put("ids", ids);
		dao.executeUpdate(hql,conditions);
	}

	@Override
	public void unlock(String id) throws Exception {
		String jql = "update Parameter as o set o.active = true where o.id = :id";
		Query query = dao.createQuery(jql);
		query.setParameter("id", id);
		query.executeUpdate();
	}

	@Override
	public void unlockAll(String[] ids) throws Exception {
		String hql = "update Parameter as o set o.active = true where o.id in (:ids)";
		Map<String, Object> conditions = new HashMap<String,Object>();
		conditions.put("ids", ids);
		dao.executeUpdate(hql,conditions);
	}

	@Override
	public void move(String id, String oldEntityID, String newEntityID) throws Exception {
		String jql = "update Parameter as o set o.entity.id = :newEntityID where o.id = :id and o.entity.id = :oldEntityID";
		Query query = dao.createQuery(jql);
		int row = 0;
		query.setParameter("id", id);
		query.setParameter("oldEntityID", oldEntityID);
		query.setParameter("newEntityID", newEntityID);
		row = query.executeUpdate();
		if(row <= 0)
			throw new AppException("未发生移动操作!");
	}

	@Override
	public void move(List<String> ids, String oldEntityID, String newEntityID) throws Exception {
		int row = 0;
		String hql = "update Parameter as o set o.entity.id = :newEntityID where o.id in (:ids) and o.entity.id = :oldEntityID";
		Map<String, Object> conditions = new HashMap<String,Object>();
		conditions.put("ids", ids);
		conditions.put("oldEntityID", oldEntityID);
		conditions.put("newEntityID", newEntityID);
		row = dao.executeUpdate(hql, conditions);
		if(row <= 0)
			throw new AppException("未发生移动操作!");
	}

	@SuppressWarnings("unchecked")
	@Transactional(propagation=Propagation.REQUIRED,readOnly = true)
	@Override
	public List<Parameter> findAllActive() throws Exception {
		Query query = dao.createNamedQuery("com.maywide.jdp.system.entities.Parameter.findAllActive");
		return query.getResultList();
	}

	@Override
	@Transactional(propagation=Propagation.NOT_SUPPORTED)
	public Page page(int first, int limit, String sort, String dir,
			Map<String, Object> conditions,Map<String, Object> parameters) throws Exception {
		Page page = null;
		String queryString = "from Parameter as o";
		String countQueryString = "select count(o.id) from Parameter as o";
		PageQueryTemplate template = new PageQueryTemplate(queryString,countQueryString,first,limit,sort,dir);
		page = dao.executePageQuery(template,conditions,null);
		
		return page;
	}
}
